package cn.edu.zjicm.wordsnet_d.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;
import cn.edu.zjicm.wordsnet_d.util.TypefaceUtil;
import cn.edu.zjicm.wordsnet_d.util.Utils;

public class VariableTextView extends View {
	private boolean learningWords = false;
	private boolean learningPhrase = false;
	private String wordsDays;
	private String phraseDays;
	private Paint paint;
	private final String leftText1 = "单词还剩";
	private final String leftText2 = "词组还剩";
	private final String textDay = "天";
	private final String textLeftDays = "距离计划完成还剩";
	private float numTextMaxHeight;

	public VariableTextView(Context context) {
		super(context);
		init();
	}

	public VariableTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public VariableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int mode = MeasureSpec.getMode(widthMeasureSpec);
		int width = Utils.getScreenWidth(getContext());
		widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (width * 3.0f / 5.0f), mode);
		super.onMeasure(widthMeasureSpec, widthMeasureSpec);
	}

	public void setData(boolean isLearningWords, boolean isLearningPhrase, int numWordsDays, int numPhraseDays) {
		this.learningWords = isLearningWords;
		this.learningPhrase = isLearningPhrase;
		this.wordsDays = numWordsDays + "";
		this.phraseDays = numPhraseDays + "";
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		float width = getWidth() - Utils.changeDipToPx(getContext(), 8);
		numTextMaxHeight = width * 4.0f / 15.0f;
		width = width * 12.0f / 13.0f;
		int centerX = (int) (getWidth() / 2.0f);
		int centerY = centerX;

		if (null == wordsDays || null == phraseDays || wordsDays.length() <= 0 || phraseDays.length() <= 0)
			return;

		if (learningWords && learningPhrase) {
			float desiredWidth = width / 3.0f;
			float leftTextSize = getTextSizeForWidth(paint, desiredWidth, wordsDays);
			float rightTextSize = getTextSizeForWidth(paint, desiredWidth, phraseDays);
			paint.setTextSize(Math.min(leftTextSize, rightTextSize));

			float leftTextWidth = getTextWidth(paint, wordsDays);
			float leftTextHeight = getTextHeight(paint, wordsDays);
			float rightTextWidth = getTextWidth(paint, phraseDays);
			float rightTextHeight = getTextHeight(paint, phraseDays);
			float leftSqrtWidth = (float) Math.sqrt(width * width - leftTextHeight * leftTextHeight);
			float rightSqrtWidth = (float) Math.sqrt(width * width - rightTextHeight * rightTextHeight);
			LogUtil.e("width=" + width + "  sqrtWidth=" + leftSqrtWidth);

			if (NightModeUtil.isNightModeNow())
				paint.setColor(Color.rgb(67, 72, 85));
			else
				paint.setColor(Color.rgb(41, 191, 141));
			// canvas.drawText(wordsDays, (centerX - leftTextWidth) / 2.0f,
			// centerY + leftTextHeight, paint);
			canvas.drawText(wordsDays, centerX - (leftSqrtWidth / 2.0f + leftTextWidth) / 2.0f, centerY + leftTextHeight, paint);

			canvas.drawText(phraseDays, centerX + (rightSqrtWidth / 2.0f - rightTextWidth) / 2.0f, centerY + rightTextHeight, paint);
			paint.setColor(Color.argb(66, 102, 102, 102));

			canvas.drawLine(centerX, centerY - leftTextHeight / 2, centerX, centerY + leftTextHeight, paint);
			paint.setColor(Color.rgb(102, 102, 102));
			desiredWidth = (width / 2 - desiredWidth) / 3.0f;
			float TextDaySize = getTextSizeForWidth(paint, desiredWidth, textDay);
			paint.setTextSize(TextDaySize);

			canvas.drawText(textDay, centerX - (leftSqrtWidth / 2.0f - leftTextWidth) / 2.0f, centerY + leftTextHeight, paint);
			// canvas.drawText(textDay, (centerX - leftTextWidth) / 2.0f +
			// leftTextWidth, centerY + leftTextHeight, paint);
			canvas.drawText(textDay, centerX + (rightSqrtWidth / 2.0f + rightTextWidth) / 2.0f, centerY + rightTextHeight, paint);

			paint.setTextSize(TextDaySize * 1.2f);
			float leftTextWidth2 = getTextWidth(paint, leftText1);
			float rightTextWidth2 = getTextWidth(paint, leftText2);
			canvas.drawText(leftText1, centerX - (leftTextWidth2 + leftSqrtWidth / 2.0f) / 2.0f, centerY - leftTextHeight / 2.0f, paint);
			canvas.drawText(leftText2, centerX + (leftSqrtWidth / 2.0f - rightTextWidth2) / 2.0f, centerY - rightTextHeight / 2.0f, paint);
		} else {
			String numStr = "";
			if (learningWords) {
				numStr = wordsDays;
			} else {
				numStr = phraseDays;
			}
			LogUtil.e("numStr=" + numStr);
			paint.setTypeface(TypefaceUtil.getInstance(getContext()).getTypeface());
			float desiredWidth = width * 2.0f / 3.0f;
			float numTextSize = getTextSizeForWidth(paint, desiredWidth, numStr);
			paint.setTextSize(numTextSize);
			float numTextWidth = getTextWidth(paint, numStr);
			float numTextHeight = getTextHeight(paint, numStr);
			if (NightModeUtil.isNightModeNow())
				paint.setColor(Color.rgb(67, 72, 85));
			else
				paint.setColor(Color.rgb(41, 191, 141));
			canvas.drawText(numStr, centerX - numTextWidth / 2.0f, centerY + numTextHeight / 8, paint);

			paint.setColor(Color.rgb(102, 102, 102));
			desiredWidth = (width - desiredWidth) / 4.0f;
			float TextDaySize = getTextSizeForWidth(paint, desiredWidth, textDay);
			paint.setTextSize(TextDaySize);
			canvas.drawText(textDay, centerX + numTextWidth / 2.0f, centerY + numTextHeight / 8, paint);

			float TextWidth2 = getTextWidth(paint, textLeftDays);
			float TextHeight2 = getTextHeight(paint, textLeftDays);

			canvas.drawText(textLeftDays, centerX - TextWidth2 / 2.0f, centerY + numTextHeight / 4 + TextHeight2 * 3.0f / 2, paint);
		}

	}

	private float getTextSizeForWidth(Paint paint, float desiredWidth, String text) {
		float testTextSize = 48f;
		paint.setTextSize(testTextSize);
		float desiredTextSize = testTextSize * desiredWidth / getTextWidth(paint, text);
		do {
			paint.setTextSize(--desiredTextSize);
		} while (getTextHeight(paint, text) > numTextMaxHeight);

		return desiredTextSize;
	}

	private float getTextWidth(Paint paint, String text) {
		if (text == null || text.length() <= 0)
			return 0;
		return paint.measureText(text);
		// Rect bounds = new Rect();
		// paint.getTextBounds(text, 0, text.length(), bounds);
		// return bounds.width();
	}

	private float getTextHeight(Paint paint, String text) {
		if (text == null || text.length() <= 0)
			return 0;
		Rect bounds = new Rect();
		paint.getTextBounds(text, 0, text.length(), bounds);
		return bounds.height();
	}
}
